package com.atguigu.flink.chapter05.source;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.connector.source.util.ratelimit.RateLimiterStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.connector.datagen.source.DataGeneratorSource;
import org.apache.flink.connector.datagen.source.GeneratorFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/*
    从数据生成器读取数据
 */
public class DataGenDemo {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port",20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        DataGeneratorSource<Integer> source = new DataGeneratorSource<>(new GeneratorFunction<Long, Integer>() {
            @Override
            public Integer map(Long value) throws Exception {
                return Math.toIntExact(value);  //Math.toIntExact 用于将long 值转换成int类型
            }
        },
                100,
                RateLimiterStrategy.perSecond(1),
                Types.INT
        );

        DataStreamSource<Integer> stream = env.fromSource(source, WatermarkStrategy.noWatermarks(), "data-source");
        stream.print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


//public class DataGenDemo {
//    public static void main(String[] args) {
//        Configuration conf = new Configuration();
//        conf.setInteger("rest.port",20000);
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
//        env.setParallelism(1);
//
//        DataGeneratorSource<Integer> source = new DataGeneratorSource<>(new GeneratorFunction<Long, Integer>() {
//            @Override
//            public Integer map(Long value) throws Exception {
//                return Math.toIntExact(value);  //Math.toIntExact 用于把long 转换为 int
//            }
//        }, 100,
//                RateLimiterStrategy.perSecond(1),
//                Types.INT);
//
//        DataStreamSource<Integer> stream = env.fromSource(source,
//                WatermarkStrategy.forMonotonousTimestamps(),
//                "data-source");
//
//        stream.print();
//
//        try {
//            env.execute();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//}

//public class DataGenDemo {
//    public static void main(String[] args) {
//        Configuration conf = new Configuration();
//        conf.setInteger("rest.port",20000);
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
//        env.setParallelism(1);
//
//        DataGeneratorSource<Integer> source = new DataGeneratorSource<>(new GeneratorFunction<Long, Integer>() {
//            @Override
//            public Integer map(Long value) throws Exception {
//                return Math.toIntExact(value);
//            }
//        }, 100,
//                RateLimiterStrategy.perSecond(1),
//                Types.INT);
//
//        DataStreamSource<Integer> stream = env.fromSource(source,
//                WatermarkStrategy.forMonotonousTimestamps(),
//                "data-source");
//
//        stream.print();
//
//        try {
//            env.execute();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//}